package org.byqh.dqeai.common;

import lombok.Data;
import org.springframework.http.HttpStatus;

@Data
public class Json<T> {
    public static final String OP_SUCCESS = "操作成功";
    public static final String OP_FAIL = "操作失败";
    public static final Boolean SUCCESS = true;
    public static final Boolean FAIL = false;
    private Boolean success = false;                        // 成功true，失败false
    private Integer status = 0;                             // 成功 200 客户端错误400+，服务端500+
    private String message = "";                            // 字符串类型
    private T data;                                          // 消息

    public Json() {
    }

    private static <T> Json<T> quickResult(Boolean success, String message, T data, Integer status) {
        Json<T> json = new Json<T>();
        json.setSuccess(success);
        json.setData(data);
        json.setMessage(message);
        json.setStatus(status);
        return json;
    }

    public static <T> Json<T> success(String message, T data, Integer status) {
        return quickResult(true, message, data, status);
    }

    public static <T> Json<T> error(String message, T data, Integer status) {
        return quickResult(false, message, data, status);
    }

    public static <T> Json<T> success(String message, T data) {
        return quickResult(true, message, data, HttpStatus.OK.value());
    }

    public static <T> Json<T> error(String message, T data) {
        return quickResult(false, message, data, HttpStatus.INTERNAL_SERVER_ERROR.value());
    }

    public static <T> Json<T> exceptionT(Exception ex) {
        return quickResult(false,
                ex.getMessage(),
                null,
                HttpStatus.INTERNAL_SERVER_ERROR.value());
    }

    public static <T> Json<T> errorT(String message) {
        return quickResult(false,
                message,
                null,
                HttpStatus.INTERNAL_SERVER_ERROR.value());
    }

    public static <T> Json<T> success(T data) {
        return quickResult(true,
                OP_SUCCESS,
                data,
                HttpStatus.OK.value());
    }

    public static Json<Boolean> success() {
        return quickResult(true,
                OP_SUCCESS,
                true,
                HttpStatus.OK.value());
    }

    public static Json<Boolean> error() {
        return Json.<Boolean>quickResult(false,
                OP_FAIL,
                false,
                HttpStatus.INTERNAL_SERVER_ERROR.value());
    }

    public static Json<Boolean> exception(Exception ex) {
        return quickResult(false,
                ex.getMessage(),
                false,
                HttpStatus.INTERNAL_SERVER_ERROR.value());
    }
}
